package com.sinothk.map.amap;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;

import com.amap.api.maps.model.LatLng;
import com.sinothk.map.amap.base.MapNaviCallback;

import java.util.ArrayList;

/**
 * <pre>
 *  创建:  梁玉涛 2019/2/28 on 10:29
 *  项目:  WuMinAndroid
 *  描述:
 *  更新:
 * <pre>
 */
public class MapNaviManager {

    public static final int DATA_FROM_BAIDU = 1; // 百度数据
    public static final int DATA_FROM_GAODE = 2; // 高德数据

    /**
     * 跳转百度地图
     */
    public static void openBaiduMap(Activity mActivity, String mAddressStr, double mLng, double mLat, int dataFromType) {
        // 百度数据->中建华府F区: 26.6094876522,106.7336647792
        if (!MapTool.hasInstalled(mActivity, "com.baidu.BaiduMap")) {
            Toast.makeText(mActivity, "请先安装百度地图客户端", Toast.LENGTH_SHORT).show();
            return;
        }


        LatLng endPoint;
        if (dataFromType == DATA_FROM_GAODE) {
            // 如果是高德数据，需要转换
            endPoint = MapTool.GCJ2BD(new LatLng(mLat, mLng));//坐标转换
        } else {
            endPoint = new LatLng(mLat, mLng);//坐标转换
        }


        Intent intent = new Intent();
        intent.setData(Uri.parse("baidumap://map/direction?destination=latlng:"
                + endPoint.latitude + "," + endPoint.longitude + "|name:" + mAddressStr + // 终点
                "&mode=driving" + // 导航路线方式
                "&src=andr.baidu.openAPIdemo"));
        mActivity.startActivity(intent); // 启动调用
    }

    /**
     * 跳转高德地图
     */
    public static void openGaodeMap(Activity mActivity, String mAddressStr, double mLng, double mLat, int dataFromType) {
        // 百度数据->中建华府F区: 26.6094876522,106.7336647792

        if (!MapTool.hasInstalled(mActivity, "com.autonavi.minimap")) {
            Toast.makeText(mActivity, "请先安装高德地图客户端", Toast.LENGTH_SHORT).show();
            return;
        }

        LatLng endPoint;
        if (dataFromType == DATA_FROM_BAIDU) {
            // 如果是百度数据，需要转换
            endPoint = MapTool.BD2GCJ(new LatLng(mLat, mLng));//坐标转换
        } else {
            endPoint = new LatLng(mLat, mLng);//坐标转换
        }

        // https://lbs.amap.com/api/amap-mobile/guide/android/route
        String stringBuffer = "androidamap://navi?sourceApplication=" + "amap" +
                "&poiname=" + mAddressStr +
                "&lat=" + endPoint.latitude +
                "&lon=" + endPoint.longitude +
                "&dev=0";

        Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(stringBuffer));
        intent.setPackage("com.autonavi.minimap");
        mActivity.startActivity(intent);
    }

    /**
     * 跳转腾讯地图
     *
     * @param mActivity
     * @param mAddressStr
     * @param mLng
     * @param mLat
     * @param dataFromType
     */
    public static void openTencentMap(Activity mActivity, String mAddressStr, double mLng, double mLat, int dataFromType) {
        if (!MapTool.hasInstalled(mActivity, "com.tencent.map")) {
            Toast.makeText(mActivity, "请先安装腾讯地图客户端", Toast.LENGTH_SHORT).show();
            return;
        }
        LatLng endPoint;
        if (dataFromType == DATA_FROM_BAIDU) {
            // 如果是百度数据，需要转换
            endPoint = MapTool.BD2GCJ(new LatLng(mLat, mLng));//坐标转换
        } else {
            endPoint = new LatLng(mLat, mLng);//坐标转换
        }

        String stringBuffer = "qqmap://map/routeplan?type=drive" +
                "&tocoord=" + endPoint.latitude + "," + endPoint.longitude + "&to=" + mAddressStr;

        Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(stringBuffer));
        mActivity.startActivity(intent);
    }


    public static void openNavi(Activity mActivity, String endAddress, double lng, double lat, int dataFromType, MapNaviCallback mapNaviCallback) {
        ArrayList<String> hasAppPnList = MapTool.hasInstalled(mActivity,
                new String[]{"com.autonavi.minimap", "com.baidu.BaiduMap", "com.tencent.map"});

        if (hasAppPnList.size() == 1) {
            String installedPkg = hasAppPnList.get(0);
            if ("com.autonavi.minimap".equals(installedPkg)) {
                openGaodeMap(mActivity, endAddress, lng, lat, dataFromType);
            } else if ("com.baidu.BaiduMap".equals(installedPkg)) {
                openBaiduMap(mActivity, endAddress, lng, lat, dataFromType);
            } else if ("com.tencent.map".equals(installedPkg)) {
                openTencentMap(mActivity, endAddress, lng, lat, dataFromType);
            }
        } else if (hasAppPnList.size() > 1) {
            if (mapNaviCallback != null) {
                mapNaviCallback.onHaveManyMapApp(hasAppPnList);
            }
        } else {
            Toast.makeText(mActivity, "手机需要高德、百度或腾讯地图App", Toast.LENGTH_SHORT).show();
        }
    }
}
